package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by hanchao on 2018/1/9.
 */
@RestController
@RequestMapping("/user/")
public class UserController {
    @Autowired
    IUserService userService;

    @PostMapping(value = "login.do")
    public ServerResponse login(String username, String password, HttpSession session){
        ServerResponse<User> response= userService.login(username,password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }
    @PostMapping(value = "logout.do")
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createdBySuccess();
    }
    @PostMapping(value = "register.do")
    public  ServerResponse<String> register(User user){
        return userService.register(user);
    }

    @PostMapping(value = "check_valid.do")
        public ServerResponse<String> checkValid(String str,String type){
        return  userService.checkValid(str,type);
    }

    @PostMapping(value = "get_user_info.do")
    public ServerResponse<User> getUSerInfo(HttpSession session){
        User user =(User) session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return ServerResponse.createdBySuccess(user);
        }else{
            return ServerResponse.createdByErrorMessage("用户未登陆，用户信息不存在");
        }
    }
    @PostMapping(value = "forget_get_question.do")
    public ServerResponse selectQuestion(String username){
        return userService.selectQuestion(username);
    }

    @PostMapping(value = "forget_check_answer.do")
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        return userService.forgetCheckAnswer(username,question,answer);
    }

    @PostMapping(value = "forget_rest_password.do")
    public ServerResponse<String> forgetRestPassword(String username,String passwordNew,String forgetToken){
        return userService.forgetRestPasswored(username,passwordNew,forgetToken);
    }

    @PostMapping(value = "reset_password.do")
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createdByErrorMessage("用户未登陆");
        }
        return userService.resetPassword(passwordOld,passwordNew,user);
    }

    @PostMapping(value = "update_information.do")
    public ServerResponse<User> updateInformation(HttpSession session,User user){
        User currentUser =(User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createdByErrorMessage("用户未登录");
        }
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
        ServerResponse<User> response = userService.updateInfomation(user);
        if(response.isSuccess()){
            response.getData().setUsername(currentUser.getUsername());
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }
    @PostMapping(value = "get_infomation.do")
    public ServerResponse<User> getInfoMation(HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createdByErrorMessage("用户未登陆,需要强制登陆status=10");
        }

        return userService.getInfomation(currentUser.getId());
    }
}
